package com.qeado.content.web;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import com.qeado.common.helper.AttributeHelper;
import com.qeado.content.model.CollegeInfo;
import com.qeado.content.service.CollegeInfoService;

@Controller
public class CollegeInfoController {
	@Autowired
	private CollegeInfoService collegeInfoService;
	
	@RequestMapping("/collegeInfoManage.ac")
	public String init(HttpServletRequest request) {
		AttributeHelper.removeSessionAttribute(request, "T_");
		List<CollegeInfo> colleageCategoryList = collegeInfoService.getColleageCategoryList();
		request.getSession().setAttribute("T_colleageCategoryList", colleageCategoryList);
		return "content/college_info_search";
	}
	
	@RequestMapping("/searchColleage.ac")
	public ModelAndView searchColleage(CollegeInfo colleageInfo) {
		List<CollegeInfo> colleageInfoList =  collegeInfoService.searchColleage(colleageInfo);
		return new ModelAndView("content/college_info_search", "colleageInfoList", colleageInfoList);
	}
	
	@RequestMapping("/newColleage.ac")
	public String newColleage() {
		return "content/college_info_manage";
	}
	
	@RequestMapping("/selectColleage.ac")
	public ModelAndView selectColleage(String id) {
		CollegeInfo colleageInfo = collegeInfoService.searchColleageById(id);
		return new ModelAndView("content/college_info_manage", "collegeInfo", colleageInfo);
	}
	
	@RequestMapping("/deleteColleage.ac")
	public ModelAndView deleteColleage(String id) {
		collegeInfoService.deleteColleage(id);
		List<CollegeInfo> colleageInfoList =  collegeInfoService.searchColleage(null);
		return new ModelAndView("content/college_info_search", "colleageInfoList", colleageInfoList);
	}
	
	@RequestMapping("/saveColleageInfo.ac")
	public ModelAndView saveColleageInfo(HttpServletRequest request, CollegeInfo colleageInfo) throws IOException {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request;
		MultipartFile file = multipartRequest.getFile("localFile");
		if(!file.isEmpty()) {
			colleageInfo.setFileName(file.getOriginalFilename());
		}
		colleageInfo.setColleageLogo(file.getBytes());
		String message = collegeInfoService.saveColleageInfo(colleageInfo);
		return new ModelAndView("content/college_info_manage", "message", message);
	}
	
	@RequestMapping("/backToColleageInfoSearch.ac")
	public String backToColleageInfoSearch() {
		return "content/college_info_search";
	}
	
	@RequestMapping("/getColleageLogo.ac")
	public void getColleageLogo(HttpServletResponse response,String id) throws UnsupportedEncodingException {
		CollegeInfo colleageInfo = collegeInfoService.getCollegeLogoById(id);
		String fileName = colleageInfo.getFileName();
		fileName = URLEncoder.encode(fileName,"UTF-8");
	    response.addHeader("Content-Disposition","attachment;filename=" + fileName);
	    OutputStream output = null;
	    ByteArrayInputStream fis = null;
	    try {
	        output = response.getOutputStream();
	        fis = new ByteArrayInputStream(colleageInfo.getColleageLogo());
	        byte[] b = new byte[1024];
	        int i = 0;
	        while((i = fis.read(b)) > 0) {
	            output.write(b, 0, i);
	        }
	        output.flush();
	    } catch(Exception e) {
	        e.printStackTrace();
	    } finally {
	        if(fis != null) { 
	        	try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	        }
	        if(output != null) {
	        	try {
					output.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
	        }
	    }
	}

}
